package com.example.demo2.test.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.example.demo2.test.model.MarketEntity;
import com.example.demo2.test.websocket.OkWebSocket;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * Description:
 *
 * @Author: my.miao
 * @Date: 2021/9/18 14:03
 */
@Slf4j
@Service
public class OkService {

    @Resource
    private ExecutorService executorService;



    public static String DEPTH = "books5";
    public static String DETAIL = "trades";
    public static String INDEX_TICKERS = "index-tickers";
    public static String MARK_PRICE = "mark-price";
    public static String FUNDING_RATE = "funding-rate";
    public static Map<String,String> symbolMap = new HashMap<>();// 对应请求数据的交易对
    private static Map<String,Long> depthTimeMap = new HashMap<>();// 150毫秒内的数据不采纳

    @Value("${ok.socket.api}")
    String api;

    OkWebSocket webSocket;

    // 此次发送数据格式
    // {
    //    "args":[
    //        {
    //            "instId":"BTC-USDT",
    //            "channel":"books5"
    //        }
    //    ],
    //    "op":"subscribe"
    //}
    public void okMarket() {
        List<MarketEntity> list = new ArrayList<>();// 查询要采集的交易对
        list.add(new MarketEntity("btc-usdt", "binance02",0,1 ));
        webSocket = OkWebSocket.createConnectionManage(this,api);// 创建websocket连接
        List<JSONObject> subDepthList = new ArrayList<>();
        List<JSONObject> subTradeList = new ArrayList<>();
        for(MarketEntity item : list) {
            String symbol = item.getSymbol().replace("_","-").toUpperCase();// 调整交易对
            symbolMap.put(symbol,item.getSymbol());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channel","books5");
            jsonObject.put("instId",symbol);
            subDepthList.add(jsonObject);
        }
        if(subDepthList.size() > 0){
            subMessage(subDepthList);// 订阅盘口
        }
        if(subTradeList.size() > 0){
            subMessage(subTradeList);// 订阅成交记录
        }
    }

    /**
     * 订阅websocket的数据
     * @param list
     */
    public void subMessage(List<JSONObject> list) {
        // 参数校验(简单的为空判断)
        if (webSocket == null) {
            throw new RuntimeException("OkWebSocket 不能为空");
        }
        if(list == null || list.size() == 0){
            throw new RuntimeException("传递的订阅信息非法");
        }
        // 组拼发送的消息
        JSONObject json = new JSONObject();
        json.put("op", "subscribe");// 设置id
        json.put("args", list);
        log.info("mes" + json);
        webSocket.send(json.toJSONString());
    }

    /**
     * 取消订阅websocket的数据
     * @param list
     */
    public void unsubMessage(List<JSONObject> list) {
        // 参数校验(简单的为空判断)
        if (webSocket == null) {
            throw new RuntimeException("OkWebSocket 不能为空");
        }
        if(list == null || list.size() == 0){
            throw new RuntimeException("传递的订阅信息非法");
        }
        // 组拼发送的消息
        JSONObject json = new JSONObject();
        json.put("op", "unsubscribe");// 设置id
        json.put("args", list);
        webSocket.send(json.toJSONString());
    }

    public void message(String text) {
        log.info("收到OK的消息了 ->" + text);
    }

    public void markPrice() {
        webSocket = OkWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject js = new JSONObject();
        js.put("op","subscribe");
        JSONObject args = new JSONObject();
        args.put("channel", "mark-price");
        args.put("instId", "BTC-USDT");
        ArrayList<JSONObject> jsonObjects = new ArrayList<>();;
        jsonObjects.add(args);
        js.put("args", jsonObjects);
        log.info("发送 ->    " + js.toJSONString() );
        webSocket.send(js.toJSONString());

    }

    public void tickers() {
        webSocket = OkWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject js = new JSONObject();
        js.put("op","subscribe");
        JSONObject args = new JSONObject();
        args.put("channel", "tickers");
        args.put("instId", "BTC-USDT");
        ArrayList<JSONObject> jsonObjects = new ArrayList<>();;
        jsonObjects.add(args);
        js.put("args", jsonObjects);
        log.info("发送 ->    " + js.toJSONString() );
        webSocket.send(js.toJSONString());
    }

    public void trades() {
        webSocket = OkWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject js = new JSONObject();
        js.put("op","subscribe");
        JSONObject args = new JSONObject();
        args.put("channel", "trades");
        args.put("instId", "BTC-USDT");
        ArrayList<JSONObject> jsonObjects = new ArrayList<>();;
        jsonObjects.add(args);
        js.put("args", jsonObjects);
        log.info("发送 ->    " + js.toJSONString() );
        webSocket.send(js.toJSONString());
    }



}
